<?php
/**
 * Date: 2018/6/12
 * Time: 9:58
 * Author: zeronlee
 */

namespace App\Services\Vuls;
use App\Models\Flaw\Vuls;
use App\Models\Flaw\VulsDelay;
use App\Models\Flaw\VulsRecord;
use Carbon\Carbon;
use App\Models\Flaw\Task;

class Delay
{
    /**
     * 申请
     * @param $request
     * @throws \Exception
     */
    public function apply ($request){
        $vul = Vuls::find($request->vuls_id);
        //验证申请权限
        if (!access()->allow("vuls-delay")){
            throw new \Exception('非法操作');
        }
        //验证漏洞状态
        if (!isset($vul->status) || $vul->status != Vuls::BEREPAIRE){
            throw new \Exception('该漏洞无需修复');
        }
        //延时在正常时间范围内没有意义
        $beginTime = VulsDelay::getBeginTime($request->vuls_id);
        if (Carbon::parse($beginTime)->diffInDays(Carbon::parse($request->delayed_at),false) <0){
            throw new \Exception('延长时间不能小于正常修复时间');
        }
        //如果没有审核，可以修改时间
        $delay = VulsDelay::where(['vuls_id'=>$request->vuls_id])->first();

        try{
            if (empty($delay)){
                //保存申请记录
                VulsDelay::apply($request);

            } else {
                if (intval($delay->agree) == 1){
                    throw new \Exception('延期已申请');
                }
                //重复申请
                $this->reApply($request);
            }

        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }

    }

    /**
     * 重复申请
     * @param $request
     * @return bool
     * @throws \Exception
     */
    public function reApply($request){
        try{

            return VulsDelay::reApply($request);
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 计算延期时间
     * @param $request
     * @return bool
     */
    public static function delayTimestamp($request){
        $vul = Vuls::find($request->vuls_id);
        $vul->delayed_at = Carbon::parse($request->delayed_at);
        return $vul->save();
    }

    /**
     * 审核
     * @param $request
     * @throws \Exception
     */
    public static function audit ($request){

        $vul = Vuls::find($request->vuls_id);
        //验证审核权限
        if (!access()->allow("vuls-auditdelay")){
            throw new \Exception('非法操作');
        }
        //验证漏洞状态
        if ($vul->status > Vuls::BEREPAIRE){
            throw new \Exception('该漏洞已修复');
        }
        //如果没有审核，可以修改时间
        $delay = VulsDelay::where(['vuls_id'=>$request->vuls_id])->first();
        if (empty($delay)){
            throw new \Exception('非法操作');
        } else{
            if ($delay->agree==1){
                throw new \Exception('该漏洞已审核');
            }
        }
        try{
            VulsDelay::audit($request);
            //驳回
            if (intval($request->agree) == 0){
                return;
            }
            //延期时间,更新table.vuls.delayed_at
            $request->delayed_at = $delay->delayed_at;
            //漏洞操作记录
            $attr = ['content' => $delay->reason,'vuls_status'=>Vuls::BEREPAIRE,'delayed_at'=>Carbon::parse($delay->delayed_at)];
            if (!VulsRecord::setRecord($request->vuls_id,$attr))
                \Log::info('vuls['.$request->vuls_id.'] update delayed_at record fail.');
            //更新vuls.delayed_at
            if (!self::delayTimestamp($request)){
                \log::info('vuls['.$request->vuls_id.'] delayed_at update fail.');
            }
            //延期后，设置重新提醒超时邮件
            if (!Task::where('vulsid', $request->vuls_id)->update([
                'sendtype' => SendEmail::ISTIMEOUT,
                'updated_at' => Carbon::now()
            ])){
                \Log::info('vuls['.$request->vuls_id.'] resetting timeout email fail.');
            }
        }catch (\Exception $e){
            throw new \Exception($e->getMessage());
        }
    }

}